package com.streetsmart;
/**
 * 
 * @author bhavani sudha
 *
 */
import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import com.streetsmart.Utilities;

/**
 * 
 * Test class for finding the curent location. 
 *
 */
public class CurrentLocationActivity extends Activity {
	LocationManager  locationManager;
	private TextView labelLongitude;
	private TextView labelLatitude;
	private TextView labelStatus;
	private TextView labelAccuracy;
	private TextView labelAddress;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.current_location);

	    labelLongitude = (TextView) findViewById(R.id.lblLongitude);
	    labelLatitude = (TextView) findViewById(R.id.lblLatitude);
	    labelStatus = (TextView) findViewById(R.id.lblStatus);
	    labelAccuracy = (TextView) findViewById(R.id.lblAccuracy);
	    labelAddress = (TextView) findViewById(R.id.lblAddress);
	    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	    LocationListener locationListener = new LocationListener() {
	    
	    	public void onLocationChanged(Location location) {
	    		labelLongitude.setText("" + location.getLongitude());
	    		labelLatitude.setText("" + location.getLatitude());
	    		labelAccuracy.setText("" + location.getAccuracy());
	    		double d[] = {location.getLatitude(), location.getLongitude()};
	    		try {
					labelAddress.setText(Utilities.getAddress(d));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	}
	    
	    	public void onProviderDisabled(String provider) {
	    		labelStatus.setText("GPS Disabled");
	    	}
	    
	    	public void onProviderEnabled(String provider) {
	    		labelStatus.setText("GPS Enabled");
	    	}
	    
	    	public void onStatusChanged(String provider, int status, Bundle extras) {
	    		labelStatus.setText("GPS Status Changed");
	    	}
	    };
	    // showLastKnownLocation();
	    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
	    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);}
	    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
	}
	private void showLastKnownLocation() {
	    Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

	    if (loc != null) {
	        labelLongitude.setText("" + loc.getLongitude());
	        labelLatitude.setText("" + loc.getLatitude());
	        labelAccuracy.setText("" + loc.getAccuracy());
	        double d[] = {loc.getLatitude(), loc.getLongitude()};
    		try {
				labelAddress.setText(Utilities.getAddress(d));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    } else {
	        labelLongitude.setText("unknown");
	        labelLatitude.setText("unknown");
	        labelAccuracy.setText("unknown");
	        labelAddress.setText("unknown");
	    }
	}
	// TODO should write this as an utility and get the results into a Location object.
}